package com.hbx.controller;


import com.hbx.aspect.annotation.SensitiveFilter;
import com.hbx.constant.ResponseStateConstant;
import com.hbx.constant.TableEnum;
import com.hbx.logs.annotation.MarkLog;
import com.hbx.logs.OperaTypeEnum;
import com.hbx.dto.BaseRequest;
import com.hbx.dto.rank.InsertOrUpdateDto;
import com.hbx.service.RankService;
import com.hbx.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-15
 */
@RestController
@RequestMapping("/rank")
@Api(tags = "头衔处理器")
public class RankController {

    @Autowired
    private RankService rankService;

    /**
     * 获得头衔列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/getRankListPage", method = RequestMethod.GET)
    @ApiOperation("获得头衔列表")
    public Result getRankListPage(BaseRequest request){
        Result r = new Result();
        if (ObjectUtils.isEmpty(request)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR).setMsg("入参错误");
            return r;
        }

        if (request.getPageNum() == null){
            request.setPageNum(1);
        }
        if (request.getPageSize() == null){
            request.setPageSize(10);
        }

        rankService.getRankListPage(request, r);
        return r;
    }

    /**
     * 删除头衔
     * @param id
     * @return
     */
    @RequestMapping(value = "/removeRank", method = RequestMethod.GET)
    @ApiOperation("删除头衔")
    @MarkLog(operate = OperaTypeEnum.DELETE,  tables = TableEnum.RANK, desc = "管理员删除头衔")
    public Result removeRank(@RequestParam("id") Integer id){
        if(ObjectUtils.isEmpty(id)){
            return Result.error().setCode(400).setMsg("id为必传项");
        }
        Result r = new Result();
        rankService.removeRank(id, r);
        return r;
    }

    /**
     * 添加或修改头衔
     * @param dto
     * @return
     */
    @RequestMapping(value = "/updateRank", method = RequestMethod.POST)
    @ApiOperation("添加或修改头衔")
    @SensitiveFilter
    @MarkLog(operate = OperaTypeEnum.UPDATE,  tables = TableEnum.RANK, desc = "管理员添加或修改头衔")
    public Result updateRank(@RequestBody @Valid InsertOrUpdateDto dto, BindingResult result){
        if(result.hasErrors()){
            return Result.error().setCode(400).setMsg(result.getFieldError().getDefaultMessage());
        }
        Result r = new Result();
        rankService.insertOrUpdateRank(dto, r);
        return r;
    }
}

